package com.aistar.service.impl;

import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.CustomerExample;
import com.aistar.rsa_aes.AESCipher;
import com.aistar.rsa_aes.Config;
import com.aistar.rsa_aes.RSACipher;
import com.aistar.rsa_aes.SecureRandomUtil;
import com.aistar.service.CustomerService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CustomerServiceImpl implements CustomerService {

    //生成aes密钥
    byte[] aesKey = Config.aesKeyStr.getBytes();


    //使用服务端公钥加密aes密钥
    byte[] encryptKey = RSACipher.encrypt(Config.SERVER_PUBLIC_KEY, aesKey);

    @Autowired
    private CustomerMapper customerMapper;

    public CustomerServiceImpl() throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException {
    }

    @Override
    public ServerResponse add(Customer customer) throws Exception {


        System.out.println("生成的aes密钥:\n" + new String(aesKey));
        System.out.println("加密后的aes密钥:\n" + new String(encryptKey));
        //使用aes密钥对数据进行加密
        String encryptCustPassWord = AESCipher.encrypt(aesKey, customer.getCustomerPassword());
        System.out.println("经过aes加密后的数据:\n" + encryptCustPassWord);

        customer.setCustomerPassword(encryptCustPassWord);




        int rows = customerMapper.insert(customer);

        if(rows == 0){
            return ServerResponse.addFailed();
        }else {
            return ServerResponse.addSuccess();
        }

    }

    @Override
    public ServerResponse modify(Customer customer) {
        return null;
    }

    @Override
    public ServerResponse delete(Integer id) {
        return null;
    }



    @Override
    public ServerResponse getById(Integer id) {

        Customer customer = customerMapper.selectByPrimaryKey(id);

        if (customer != null && customer.getStatus().equals("1"))
            return ServerResponse.getSuccess(customer);
        return ServerResponse.getFailed("查询无此用户");
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }


    @Override
    public ServerResponse getByTel(String telno) {

        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andCustomerTelnoEqualTo(telno);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);

        if(customerList != null && customerList.size()>0)
            return ServerResponse.registerSuccess("用户已存在");
        else
        return ServerResponse.registerFailed("用户不存在");
    }

    @Override
    public ServerResponse getByTelAndPw(String telno, String passWord) throws Exception {

        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerTelnoEqualTo(telno);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            Customer customer = customerList.get(0);
            //使用服务端私钥对加密后的aes密钥解密
            byte[] aesKey1 = RSACipher.decrypt(Config.SERVER_PRIVATE_KEY, encryptKey);
            System.out.println("解密后的aes密钥:\n" + new String(aesKey1));
            //使用aes私钥解密密文
            String customerPassword = AESCipher.decrypt(aesKey1, customer.getCustomerPassword());
            System.out.println("经过aes解密后的数据:\n" + customerPassword);
            if (passWord.equals(customerPassword)) {
                customer.setCustomerPassword(passWord);
                return ServerResponse.loginSuccess(customer);
            } else {
                return ServerResponse.loginFailed();
            }
        } else{
            return ServerResponse.loginFailed();
        }
        /*criteria.andCustomerPasswordEqualTo(passWord);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);

        if(customerList != null && customerList.size()>0)
            return ServerResponse.loginSuccess(customerList.get(0));
        else
            return ServerResponse.loginFailed();
*/
    }


    @Override
    public ServerResponse getByNameAndPw(String name, String passWord) throws Exception {

        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(name);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            Customer customer = customerList.get(0);
            //使用服务端私钥对加密后的aes密钥解密
            byte[] aesKey1 = RSACipher.decrypt(Config.SERVER_PRIVATE_KEY, encryptKey);
            System.out.println("解密后的aes密钥:\n" + new String(aesKey1));
            //使用aes私钥解密密文
            String customerPassword = AESCipher.decrypt(aesKey1, customer.getCustomerPassword());
            System.out.println("经过aes解密后的数据:\n" + customerPassword);
            if (passWord.equals(customerPassword)) {
                customer.setCustomerPassword(passWord);
                return ServerResponse.loginSuccess(customer);
            } else {
                return ServerResponse.loginFailed();
            }
        } else{
            return ServerResponse.loginFailed();
        }
 /*       criteria.andCustomerPasswordEqualTo(passWord);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);

        if(customerList != null && customerList.size()>0)
            return ServerResponse.loginSuccess(customerList.get(0));
        else
            return ServerResponse.loginFailed();*/


    }

    @Override
    public ServerResponse getByAccountAndPw(String type, String account, String passWord) throws Exception {

        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        System.out.println(type);
        if(type.equals("0")){
            //说明是手机号登录
            System.out.println("手机号");
            criteria.andCustomerTelnoEqualTo(account);
        }else{
            //说明是名字
            System.out.println("用户名");
            criteria.andCustomerNameEqualTo(account);
        }

        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if(customerList != null && customerList.size()>0){
            Customer customer = customerList.get(0);
        //使用服务端私钥对加密后的aes密钥解密
        byte[] aesKey1 = RSACipher.decrypt(Config.SERVER_PRIVATE_KEY, encryptKey);
        System.out.println("解密后的aes密钥:\n" + new String(aesKey1));
        //使用aes私钥解密密文
        String customerPassword = AESCipher.decrypt(aesKey1, customer.getCustomerPassword());
        System.out.println("经过aes解密后的数据:\n" + customerPassword);
        if(passWord.equals(customerPassword)){
            customer.setCustomerPassword(passWord);
            return ServerResponse.loginSuccess(customer);
        }else {
            return ServerResponse.loginFailed();
        }

      //  criteria.andCustomerPasswordEqualTo(passWord);
//        List<Customer> customerList = customerMapper.selectByExample(customerExample);

/*        if(customerList != null && customerList.size()>0)
            return ServerResponse.loginSuccess(customerList.get(0));
        else
            return ServerResponse.loginFailed();*/
        } else{
            return ServerResponse.loginFailed();
        }
    }

    @Override
    public Customer getByCookie(String account, String passWord) throws Exception {
        String regex = "^(1[3-9]\\d{9}$)";
        ServerResponse serverResponse ;
        if(Pattern.matches(regex, account)){
          serverResponse =  getByTelAndPw(account,passWord);
        }else {
          serverResponse =  getByNameAndPw(account,passWord);
        }
        if(serverResponse.getStatus() == 1){
            Customer customer = (Customer) serverResponse.getData();
            return customer;
        }
        else return null;

    }
}
